<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<style>
    #upload_file_btn{height: 30px;line-height: 28px;width: 100%;cursor: pointer;background-color: #1ca5ff!important;}
</style>
<body>
    <form class="layui-form layui-form-pane" id="operatePage">
        <div class="page-title">
            <div class="page-title-con">
                <span>编辑</span>
                <div class="move_title"></div>
                <?php include "../public/sub.php";?>
            </div>
        </div>

        <div class="main-container">
            <div class="block-generic">
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label">位置</label>
                            <div class="layui-input-block">
                                <input type="text" id="pid_name" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12">
                            <label class="layui-form-label"><text>*</text>名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item height_auto">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12" >
                            <label class="layui-form-label label_textarea">备注</label>
                            <div class="layui-input-block">
                                <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item height_auto">
                    <div class="layui-row layui-col-space10">
                        <div class="layui-col-md12" >
                            <label class="layui-form-label" style="height: 75px">
                                <button type="button" class="layui-btn" id="upload_file_btn"><i class="fa fa-plus jia"></i>上传附件</button>
                            </label>
                            <div class="layui-input-block">
                                <input type="hidden" id="fileid" name="fileid">
                                <input type="text" id="filename" name="filename" clearable readonly autocomplete="off" class="layui-input">
                                <input type="file" id="uploadfile" value="" style="display: none"/>
                                <div style="color: #aaa;margin-top: 5px">重新上传附件将覆盖源文件</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        </div>
    </form>
    <script>
        var param_id = getQueryVariable('id') || 0;
        var param_pid = 0;

        $("#operatePage").validate({
            rules: {
                name: {required: true},
            },
            messages:{
                name: {required: "请输入"},
            }
        });


        layui.use(['laytp', 'form'], function() {
            var form = layui.form, layer = layui.layer;
            render_input_attr();
            facade.ajax({type: 'get', url: ask_temp_ban_edit, data:{id: param_id}}).done(function(res){
                if (res.code === 2000) {
                    var data = res.data.data;
                    $("#pid_name").val(data.pid_name);
                    $("#name").val(data.name);
                    $("#explain").val(data.explain);

                    param_pid = data.pid;

                    render_from();
                }
            });

            $("#upload_file_btn").click(function () {
                $("#uploadfile").click();
            });
            $("#uploadfile").change(function () {
                var loading = layui.layer.load(0, {shade: 0.1});
                let fd = new FormData();
                fd.append('file', this.files[0]);
                fd.append('token', facade.getCookie('token'));
                $.ajax({
                    url: server_api_name+ask_engin_bao_chong,
                    type: "POST",
                    data: fd,
                    contentType: false,
                    processData: false,
                    dataType: "json",
                    success: function (res) {
                        $("#uploadfile").val();
                        layui.layer.close(loading);
                        if (res.code === 2000) {
                            facade.success("提交成功!");
                            $("#fileid").val(res.data.id);
                            $("#filename").val(res.data.name);
                        }else{
                            facade.error(res.msg);
                        }
                    },
                    error: function (res) {
                        $("#uploadfile").val();
                        layui.layer.close(loading);
                        facade.error("网络错误");
                    },
                })
            });



            form.on('submit(add)', function(data){
                var validRes = $("#operatePage").valid();
                if(!validRes){return false;}

                if(!data.field.filename){
                    data.field.fileid = 0
                }
                delete data.field.filename

                data.field.id = param_id;
                facade.ajax({url: ask_temp_ban_edit, data:data.field}).done(function(res){
                    if (res.code === 2000) {
                        facade.success("提交成功!");
                        setTimeout(() => {
                            try {
                                parent.get_mu_info(param_pid, param_id);
                            }catch (e) {

                            }
                            layer_close_opt();
                        }, 1500);
                    }
                });

                return false;
            });

        });

    </script>
</body>
</html>
